//
//  DealPswViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/3.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "DealPswViewController.h"

@interface DealPswViewController ()
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (weak, nonatomic) IBOutlet UIButton *CodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *DealTF;
@property (weak, nonatomic) IBOutlet UITextField *ReDealTF;
@property (assign, nonatomic) NSInteger timeNmum;
@property (strong, nonatomic) NSTimer *getTime;
@end

@implementation DealPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"交易密码";
    _PhoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _CodeTF.keyboardType = UIKeyboardTypeNumberPad;
    _DealTF.keyboardType = UIKeyboardTypeNumberPad;
    _ReDealTF.keyboardType = UIKeyboardTypeNumberPad;
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    _timeNmum = 60;
    [_CodeBtn.layer setCornerRadius:15.0];
    _CodeBtn.layer.masksToBounds = YES;
    
    [self judgeLoginEnabled];
}

-(void)judgeLoginEnabled{
    
    [_PhoneTF.rac_textSignal subscribeNext:^(NSNumber *result) {
        if(String(result).length == 11){
            [_CodeBtn setBackgroundColor:[UIColor redColor]];
            _CodeBtn.enabled = YES;
        }else{
            [_CodeBtn setBackgroundColor:[UIColor grayColor]];
            _CodeBtn.enabled = NO;
        }
        
    }];
}

- (void)begainTime
{
    if (_timeNmum > 0) {
        _timeNmum--;
        _CodeBtn.enabled = NO;
        [_CodeBtn setTitle:[NSString stringWithFormat:@"%ldS",(long)_timeNmum] forState:UIControlStateNormal];
    }else{
        _timeNmum = 0;
        if ([_getTime isValid]) {
            [_getTime invalidate];
            _getTime = nil;
        }
        [_CodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        _CodeBtn.enabled = YES;
    }
}

- (IBAction)CodeClick:(UIButton *)sender {
    if (_PhoneTF.text.length == 0 || _PhoneTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    
    if (_PhoneTF.text.length == 11){
        _timeNmum = 60;
        NSDictionary *dic = @{@"tel":_PhoneTF.text,@"type":@"8"};
        
        [THWManager PostHttp:API(MineDealPSWPhone) Parameters:dic Success:^(id responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                    _getTime =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(begainTime) userInfo:nil repeats:YES];
                    _CodeBtn.enabled = NO;
                }else{
                    ALERT(responseObject[@"msg"]);
                }
            }
        } andFalse:^(NSError *error) {
        }];
    }
}

- (IBAction)SubmitClick:(UIButton *)sender {
    if (_PhoneTF.text.length != 11 || _PhoneTF.text.length == 0  || _CodeTF.text.length == 0 || _DealTF.text.length == 0 || _ReDealTF.text.length == 0) {
        [self.view Message:@"请输入必填项"];
        return;
    }
    
    if (_DealTF.text.length != 6 || _ReDealTF.text.length != 6) {
        [self.view Message:@"支付密码应为六位数字"];
        return;
    }
    
    if (![_DealTF.text isEqualToString:_ReDealTF.text]) {
        [self.view Message:@"两次密码不一致"];
        return;
    }
    
    [self.view loadingOnAnyView];
    NSData *PSD = [_DealTF.text
                   dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64PSD = [PSD base64EncodedStringWithOptions:0];
    
    NSData *rePSD = [_ReDealTF.text
                   dataUsingEncoding:NSUTF8StringEncoding];
    NSString *reBase64PSD = [rePSD base64EncodedStringWithOptions:0];
    
    [THWManager PostHttp:API(MineEditDeal) Parameters:@{@"userToken":[Utils getUserToken], @"userPhone":_PhoneTF.text, @"phone_codes":_CodeTF.text, @"payPwd":base64PSD, @"repayPwd":reBase64PSD} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
            [self.view Message:responseObject[@"msg"]];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_PhoneTF resignFirstResponder];
    [_CodeTF resignFirstResponder];
    [_DealTF resignFirstResponder];
    [_ReDealTF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
